package net.fdukedom.epicurus.pm.actions;

import net.fdukedom.epicurus.presentation.actions.AbstractForm;
import net.fdukedom.epicurus.domain.util.UserHelper;
import net.fdukedom.epicurus.domain.entity.User;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;

/**
 * Bean for register form.
 *
 * @author Dmitry Y. Kochelaev
 */
public class ComposeForm extends AbstractForm {

    /**
	 * User to.
	 */
	private String to;

    /**
	 * Message title.
	 */
	private String title;

    /**
	 * Message body.
	 */
	private String body;

    private UserHelper userHelper = UserHelper.getInstance();

    public ActionErrors doValidate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        User toUser = userHelper.findByName(to);
        if (toUser == null) {
            errors.add("error.compose.login", new ActionMessage("pm.compose.errors.badLogin"));
        }
        if (title.length() == 0) {
            errors.add("error.compose.title", new ActionMessage("pm.compose.errors.emptyTitle"));
        }
        if (body.length() == 0) {
            errors.add("error.compose.body", new ActionMessage("pm.compose.errors.emptyBody"));
        }
        return errors;
    }

    public void doReset(ActionMapping mapping, HttpServletRequest request) {
        to = "";
        body = "";
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
